package cn.com.jsepc.rep.comvert;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.ServletException;


import com.jarp.db.DAO;

public class Convert {

	public String doConvert() {
		String res = "";
		double total=0;
		DAO dao = new DAO();
		String sql = "SELECT * FROM tb_report t WHERE t.SOURCETYPE=2 and t.REFFACT is not null";
		Date da=new Date();
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String thistime=format.format(da);
		try {
			Vector v = dao.getDataSet("Jarp", sql);
			System.out.println("总共需要检索"+dao.getDataSetCount("Jarp",sql)+"个事实表");
			List<ReportVO> replist = new ArrayList<ReportVO>();
			ArrayList<String> array=new ArrayList<String>();
			if (v != null) {
				for (int i = 0; i < v.size(); i++) {
					ReportVO rep = new ReportVO();
					Hashtable hrep = (Hashtable) v.get(i);
					String refacttable = (String) hrep.get("REFFACT");
					String refsql = "SELECT * FROM " + refacttable;
					int tablecount=dao.getDataSetCount("Jarp", refsql);
					total+=tablecount;
					System.out.println("正在检索"+refacttable+"表,这个表有"+tablecount+"条数据");
					int totalpage=tablecount%1000>0?tablecount/1000+1:tablecount/1000;
					for(int j=1;j<=totalpage;j++){
			            Vector refv = dao.getDataSet("Jarp", refsql,j,1000);   
						System.out.println("这个结果集共有"+refv.size()+"条数据,正在写入");
						List<DemVO> reflist = new ArrayList<DemVO>();
						if (refv != null) {
							for (int j2 = 0; j2 < refv.size(); j2++) {
							
								DemVO dem = new DemVO();
								Hashtable refhash = (Hashtable) refv.get(j2);
								String org = (String) refhash.get("ORGID");
								String year = (String) refhash.get("YEARID");
								String type = (String) refhash.get("TYPEID");
								String repval = (String) refhash.get("REPVAL");
								String repcode = (String) hrep.get("REPCODE");
								String time = (String) hrep.get("UPDATETIME");
								
								String updatetime=this.frequence(Integer.valueOf(time));
								String trepval = (String) refhash.get("TREPVAL");

								String CreateData="INSERT INTO report_temp " +
										"(target_code,target_value,grainTime,acountType,frequence,area,nowvalue,creatTime) VALUES" +
										"('"+repcode+"','"+repval+"','"+year+"','"+type+"','"+updatetime+"','"+org+"','"+trepval+"','"+thistime+"')";
								//System.out.println(CreateData);
								array.add(CreateData);
								//dao.execCommand("Jarp",CreateData);
							}
							boolean b=dao.execCommand("Jarp", array);
							array.clear();
						}
					}
					
				}
				System.out.println("总共有"+total+"条数据");
				
//				if(b==true){
//					System.out.print("操作成功");
//					res="成功！";
//				}
//				else{
//					System.out.print("操作失败");
//					res="失败！";
//				}
			}
		} catch (ServletException e) {
			e.printStackTrace();
		}
		return res;
	}

	public String frequence(int v){
		
		if (v == 1) {
			return "年";
			} else if (v == 2) {
				return "季";
			} else if (v == 3) {
				return "月";
				} else if (v == 4) {
					return "日";
					} else if (v == 5) {
						return "时";
						} else if (v == 6) {
							return "分";
							}
		return null;
	}
}
